home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / hce.lha / HCE / Examples / AMIGA / Pools / Gfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  12.8 KB  |  426 lines

  1. /*
  2.  * Copyright (c) 1994. Author: Jason Petty.
  3.  *
  4.  * Permission is granted to anyone to use this software for any purpose
  5.  * on any computer system, and to redistribute it freely, with the
  6.  * following restrictions:
  7.  * 1) No charge may be made other than reasonable charges for reproduction.
  8.  * 2) Modified versions must be clearly marked as such.
  9.  * 3) The authors are not responsible for any harmful consequences
  10.  *    of using this software, even if they result from defects in it.
  11.  *
  12.  *
  13.  *     Gfx.c:
  14.  *
  15.  *            Opens libraries/screen/window and printer device.
  16.  *            Does all graphics related stuff.
  17.  *            Does Help messages.
  18.  *            Does all Printer related stuff.
  19.  */
  20.  
  21. #include <exec/types.h>
  22. #include <exec/errors.h>
  23. #include <exec/memory.h>
  24. #include <exec/io.h>
  25. #include <libraries/dos.h>
  26. #include <devices/printer.h>
  27.  
  28. #ifndef  GRAPHICS_GFXBASE_H
  29. #include <graphics/gfxbase.h>
  30. #endif
  31.  
  32. #ifndef  INTUITION_INTUITION_H
  33. #include <intuition/intuition.h>
  34. #endif
  35.  
  36. #include <graphics/gfxmacros.h>
  37.  
  38. #ifndef STDIO_H
  39. #include <clib/stdio.h>
  40. #endif
  41.  
  42. #ifndef STRING_H
  43. #include <clib/string.h>
  44. #endif
  45.  
  46. #include "pools.h"
  47.  
  48.    /**** All Structure definitions start Here *****/
  49.  
  50. chip struct NewScreen my_new_screen=
  51. {
  52.   0,            /* LeftEdge */
  53.   0,            /* TopEdge  */
  54.   641,
  55.   266,
  56.   4,            /* 4 = 16 colours.   */
  57.   1,            /* DetailPen Text should be printed with colour reg. 1   */
  58.   5,            /* BlockPen  Blocks should be printed with colour reg. 0 */
  59.   HIRES,        /* ViewModes.                            */
  60.   CUSTOMSCREEN|SCREENQUIET, /* Type. Your own customized screen. */
  61.   NULL,         /* Font      Default font.               */
  62.   NULL,         /* UBYTE Pools-V1.0.  Title.               */
  63.   NULL,         /* Gadget                                */
  64.   NULL          /* BitMap    No special CustomBitMap.    */
  65. };
  66.  
  67. /* Covers hole of screen and is used for graphics. */
  68. struct NewWindow gfx_new_win=
  69. {
  70.   0,1,
  71.   641, 259,                             /* Use screens sizes.   */
  72.   1,0,                          /* Detailpen, Blockpen. */
  73.   IDCMP_CLOSEWINDOW|IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP,
  74.   WFLG_CLOSEGADGET|WFLG_ACTIVATE|WFLG_GIMMEZEROZERO|WFLG_BORDERLESS,
  75.   NULL,                                 /* First gadget.        */
  76.   NULL,                                 /* Image checkmark.     */
  77.   NULL,                                 /* Title.               */
  78.   NULL,                                 /* Screen pointer.      */
  79.   NULL,                                 /* Custom Bitmap.       */
  80.   120,88,                         /* Min W,H sizes.       */
  81.   641,259,                         /* Max W,H sizes.       */
  82.   CUSTOMSCREEN                          /* Type.                */
  83. };
  84.  
  85. /* This is a general purpose shared window. */
  86. struct NewWindow g_new_window=
  87. {
  88.   0,             /* LeftEdge   */
  89.   1,             /* TopEdge    */
  90.   640,           /* Width      */
  91.   200,           /* Height     */
  92.   2,3,           /* DetailPen,BlockPen */
  93.   IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP,
  94.   WFLG_SMART_REFRESH|WFLG_CLOSEGADGET|WFLG_ACTIVATE,
  95.   NULL,          /* FirstGadget */
  96.   NULL,          /* CheckMark   */
  97.   NULL,          /* Title.      */
  98.   NULL,          /* Screen      */
  99.   NULL,          /* BitMap      */
  100.   20,            /* MinWidth    */
  101.   20,            /* MinHeight   */
  102.   641,           /* MaxWidth    */
  103.   259,           /* MaxHeight   */
  104.   CUSTOMSCREEN   /* Type        */
  105. };
  106.  
  107. union printerIO             /* Printer Request Block. */
  108. {
  109.   struct IOStdReq ios;
  110.   struct IODRPReq iodrp;
  111.   struct IOPrtCmdReq iopc;
  112. };
  113.  
  114.  
  115. union printerIO *prt_req=0;
  116. struct MsgPort *prt_reply=0;
  117. UWORD prt_error=TRUE;             /* Printer Device error. */
  118. extern struct Gadget *sl_gadlist; /* Head of 'choose league' Gadget list.*/
  119. struct Window *g_window=0;        /* (Shared Window)   */
  120. struct Window *gfx_window=0;      /* (graphics window) */
  121. struct Screen *my_screen=0;
  122. struct GfxBase *GfxBase=0;
  123. long IntuitionBase=0;
  124. long GadToolsBase=0;
  125.  
  126.   /**** Global Variables Here ****/
  127.  
  128. static WORD font_width, font_height;
  129. char PR_BUF[90]; /* General purpose buffer. */
  130.  
  131. /********************** Rest of code = FUNCTIONS *********************/
  132.  
  133.  /* Start() must be called at beginning; returns window pointer - if 0,
  134.   * program has failed ... everything already cleaned up here, main ()
  135.   * must do its own cleaning up then exit */
  136.  
  137. struct Window *start ()
  138. {
  139.   /* Libraries. */
  140.  if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0)))
  141.   goto FAILED;
  142.  if (!(IntuitionBase=(long)OpenLibrary("intuition.library",0)))
  143.   goto FAILED;
  144.         if (!(GadToolsBase=(long)OpenLibrary("gadtools.library", 36))) {
  145.        /*  printf("Could not Open gadtools.library - V36 or Higher!!\n"); */
  146.                 goto FAILED;
  147.                 }
  148.  
  149.    /* Screen */
  150.         if (!(my_screen = (struct Screen *)OpenScreen( &my_new_screen)))
  151.                 goto FAILED;
  152.  
  153.             g_new_window.Screen=my_screen;  /* Attach g Win to Screen.  */
  154.             gfx_new_win.Screen=my_screen;   /* Attach gfx Win to Screen.*/
  155.  
  156.     /* GadTools */
  157.         if (!(Alloc_VisualInfoA())) /* Get Visual info for gad tools. */
  158.                  goto FAILED;
  159.         if (!(Alloc_L_Gadgets()))   /* Alloc, League Gadgets */
  160.                  goto FAILED;
  161.  
  162.             gfx_new_win.FirstGadget = (struct Gadget *)sl_gadlist;
  163.  
  164.     /* Gfx Window. */
  165.        if (!(gfx_window = (struct Window *)OpenWindow(&gfx_new_win))) {
  166.               /* printf("No gfx_window!!\n"); */
  167.                  goto FAILED;
  168.         }
  169.     /* Set up 'my_screen' and 'gfx_window' Graphics. */
  170.         (void)Set_Graphics();
  171.  
  172.        font_width=GfxBase->DefaultFont->tf_XSize; /* May need these. */
  173.        font_height=GfxBase->DefaultFont->tf_YSize;
  174.  
  175.    /* Printer stuff (note: actual printer device only opened when needed) */
  176.         if (!(prt_reply = (struct MsgPort *)CreatePort( NULL, 0))) {
  177.                /* printf("Could not Open - Printer reply port!!\n"); */
  178.                 goto FAILED;
  179.                 }
  180.         if (!(prt_req = (union printerIO *) 
  181.                 CreateExtIO(prt_reply, sizeof(union printerIO)))) {
  182.              /* printf("Could not Create - Printer ExtIO!!\n");  */
  183.                 goto FAILED;
  184.                 }
  185.  
  186.         return (gfx_window);
  187.  
  188. FAILED:
  189.         finish ();
  190.         return (0L);
  191. }
  192.  
  193.  /* Call finish() at end, if start() was successful. */
  194. void finish ()
  195. {
  196.         if (!prt_error)                 /* Close printer device. */
  197.                 CloseDevice( prt_req );
  198.         if (prt_req)
  199.                 DeleteExtIO( prt_req, sizeof(union printerIO) );
  200.         if (prt_reply)
  201.                 DeletePort( prt_reply);
  202.         if (gfx_window)
  203.             CloseWindow (gfx_window);
  204.  
  205.          Close_GWind();      /* Close g window. (Tests for opened).      */
  206.          Free_GT_Gadgets();  /* Free gad memory. (Tests if allocated).   */
  207.          Free_VisualInfo();  /* Free visual memory. (Tests if allocated).*/
  208.  
  209.         if (my_screen)
  210.                 CloseScreen (my_screen);
  211.         if (GadToolsBase)
  212.                 CloseLibrary(GadToolsBase);
  213.  if (GfxBase)
  214.   CloseLibrary (GfxBase);
  215.  if (IntuitionBase)
  216.   CloseLibrary (IntuitionBase);
  217. }
  218.  
  219. /********** START OF PRINTER FUNCTIONS ******************/
  220.  
  221. void PrtError(error) /* Prints the appropriate printer error message.*/
  222. BYTE error;          /* (g_window) result box */
  223. {
  224.   Clear_RBOX();
  225.  
  226.   switch( error ) /* Errors found in 'exec/errors.h' & 'devices/printer.h' */
  227.   {
  228.   case IOERR_ABORTED:
  229.           RB_Msg("ERROR: The printer request was aborted!",0);
  230.           break;
  231.   case IOERR_NOCMD:
  232.           RB_Msg("ERROR: Unknown printer command was sent!",0);
  233.           break;
  234.   case IOERR_BADLENGTH:
  235.           RB_Msg("ERROR: Bad length in the printer CMD - DATA!",0);
  236.           break;
  237.   case PDERR_CANCEL:
  238.           RB_Msg("All Printing Cancelled!",0);
  239.           break;
  240.   case PDERR_NOTGRAPHICS:
  241.           RB_Msg("ERROR: Printer doesn`t support Graphics!.",0);
  242.           break;
  243.   case PDERR_BADDIMENSION:
  244.           RB_Msg("ERROR: Printer dimension is not valid!",0);
  245.           break;
  246.   case PDERR_INTERNALMEMORY:
  247.           RB_Msg("No memory for internal printer functions!",0);
  248.           break;
  249.   case PDERR_BUFFERMEMORY:
  250.           RB_Msg("No memory for the printer buffer!",0);
  251.           break;
  252.   default:
  253.           RB_Msg("ERROR: Unkown printer error received!",0);
  254.           break;
  255.   }
  256. }
  257.  
  258. BYTE DO_PrtText(data)      /* Sends translated Text to the printer. */
  259. char *data;                /* Pointer to text. (Uses WB prefs).     */
  260. {
  261.  if(prt_error) {           /* Printer device not open?. */
  262.      if ((prt_error = OpenDevice("printer.device", 0, prt_req, 0))) {
  263.               return((BYTE)prt_error);
  264.               }
  265.    }
  266.   prt_req->ios.io_Command = CMD_WRITE;
  267.   prt_req->ios.io_Data = (APTR)data;
  268.   prt_req->ios.io_Length = -1;
  269.   return( (BYTE) DoIO( prt_req ) ); /* return, 0 = Ok else error. */
  270. }
  271.  
  272. /********* END OF PRINTER FUNCTIONS *******************/
  273.  
  274.  
  275. /********* GRAPHICS FUNCTIONS ***********/
  276.  
  277. void Set_Graphics()
  278. {
  279.   SetRGB4( s_vp,  0, 0x0, 0x0, 0x0 );   /* black.        */
  280.   SetRGB4( s_vp,  1, 0xF, 0xF, 0xF );   /* white.        */
  281.   SetRGB4( s_vp,  2, 0xF, 0xF, 0x0 );   /* Light Yellow. */
  282.   SetRGB4( s_vp,  3, 0xF, 0x3, 0x2 );   /* Red.          */
  283.   SetRGB4( s_vp,  4, 0x5, 0x8, 0xD );   /* Med blue.     */
  284.   SetRGB4( s_vp,  5, 0x9, 0xC, 0xF );   /* Light blue.   */
  285.   SetRGB4( s_vp,  6, 0x9, 0xF, 0x9 );   /* light-green.  */
  286.   SetRGB4( s_vp,  7, 0x4, 0x4, 0x4 );   /* Dark-grey.    */
  287.   SetRGB4( s_vp,  8, 0x5, 0x5, 0x5 );   /* .........     */
  288.   SetRGB4( s_vp,  9, 0x6, 0x6, 0x6 );   /* .........     */
  289.   SetRGB4( s_vp,  10, 0x7, 0x7, 0x7 );  /* .........     */
  290.   SetRGB4( s_vp,  11, 0x8, 0x8, 0x8 );  /* .........     */
  291.   SetRGB4( s_vp,  12, 0x9, 0x9, 0x9 );  /* .........     */
  292.   SetRGB4( s_vp,  13, 0xA, 0xA, 0xa );  /* .........     */
  293.   SetRGB4( s_vp,  14, 0xB, 0xB, 0xB );  /* .........     */
  294.   SetRGB4( s_vp,  15, 0xC, 0xC, 0xC );  /* Light-grey.   */
  295.  
  296.   SetDrMd( gfx_rp, JAM2);
  297.   SetDrMd( g_rp, JAM2);
  298. }
  299.  
  300. /* GFX WINDOW FUNCS. */
  301. void gfx_TXT(s,x,y)  /* Simple text printing func. */
  302. char *s;
  303. {
  304.   Move(gfx_rp,x,y);
  305.   Text(gfx_rp,s,strlen(s));
  306. }
  307.  
  308. void gfx_FPEN(p) /* Set front pen */
  309. int p;
  310. {
  311.  SetAPen(gfx_rp,p);
  312. }
  313.  
  314. void gfx_BPEN(p) /* Set back pen */
  315. int p;
  316. {
  317.  SetBPen(gfx_rp,p);
  318. }
  319.  
  320. /* G WINDOW FUNCS. */
  321. void g_TXT(s,x,y)  /* Simple text printing func. */
  322. char *s;
  323. {
  324.   Move(g_rp,x,y);
  325.   Text(g_rp,s,strlen(s));
  326. }
  327.  
  328. void g_FPEN(p) /* Set front pen */
  329. int p;
  330. {
  331.  SetAPen(g_rp,p);
  332. }
  333.  
  334. void g_BPEN(p) /* Set back pen */
  335. int p;
  336. {
  337.  SetBPen(g_rp,p);
  338. }
  339.  
  340. void Draw_RBOX()  /* Draw a box to put pools results in. */
  341. {
  342.   g_FPEN(10);
  343.   g_BPEN(10);
  344.   RectFill(g_rp, 2, RS_Y-30, 638, 253);  /* Result box.*/
  345.  
  346.   g_FPEN(0);
  347. /* These make up an inside black border to box. */
  348.   Move(g_rp, 30, RS_Y-20);
  349.   Draw(g_rp, 30, RS_Y+60);     /* line bottom left side of wind.*/
  350.   Move(g_rp, 610, RS_Y-20);
  351.   Draw(g_rp, 610, RS_Y+60);    /* line bottom right side of wind.*/
  352.   Move(g_rp, 30, RS_Y-20);
  353.   Draw(g_rp, 610, RS_Y-20);    /* line at bottom edge of gads. */
  354.   Move(g_rp, 30, RS_Y+60);
  355.   Draw(g_rp, 610, RS_Y+60);    /* line near bottom of wind.*/
  356.  
  357.   g_TXT(" Results ",284, RS_Y-16); /* Box Title. */
  358.   g_FPEN(5);
  359.   g_BPEN(0);
  360.   Move(g_rp, 0, RS_Y-31);
  361.   Draw(g_rp, 640, RS_Y-31);    /* Add strip of light to top of box */
  362.  
  363.   Move(g_rp, 0, RS_Y-42);
  364.   Draw(g_rp, 640, RS_Y-42);    /* Line underneath league Gads.*/
  365.   g_FPEN(1);
  366.   g_BPEN(10);
  367. }
  368.  
  369. void Help() /* Simple help message. */
  370. {
  371.   RB_Msg("USE THE MOUSE TO INPUT FIXTURES FROM YOUR COUPON.",0);
  372.   RB_Msg("THE 'Printer' GADGET SENDS EACH RESULT TO THE PRINTER WHEN IN",2);
  373.   RB_Msg("THE 'ON' POSITION.",3);
  374.   RB_Msg("THE 'Print-Heading' GADGET PRINTS THE POOLS HEADING, READY",4);
  375.   RB_Msg("FOR THE RESULTS TO FOLLOW.(Only if 'printer' gad is 'ON')",5);
  376. }
  377.  
  378. void RB_Msg(msg,line)  /* Print message in Result box area. */
  379. char *msg;             /* line is 0-5. */
  380. int line;
  381. {
  382.  int off=0;
  383.           switch(line) {    /* Max of 6 lines to print on. */
  384.                  case 0:
  385.                         break;
  386.                  case 1:
  387.                         off = 10;
  388.                         break;
  389.                  case 2:
  390.                         off = 20;
  391.                         break;
  392.                  case 3:
  393.                         off = 30;
  394.                         break;
  395.                  case 4:
  396.                         off = 40;
  397.                         break;
  398.                  case 5:
  399.                         off = 50;
  400.                         break;
  401.                  default:
  402.                         break;
  403.                  }
  404.  g_FPEN(1);
  405.  g_BPEN(10);
  406.  g_TXT(msg, RS_X, RS_Y+off);
  407. }
  408.  
  409. void Clear_RBOX()     /* Clear the result box area.*/
  410. {                     /* 6 lines.  */
  411. int i;
  412.     g_BPEN(10);
  413.    for(i=0;i<68;i++)
  414.     PR_BUF[i] = ' ';
  415.     PR_BUF[i] = '\0';
  416.  
  417.     g_TXT(PR_BUF,RS_X,RS_Y);
  418.     g_TXT(PR_BUF,RS_X,RS_Y+10);
  419.     g_TXT(PR_BUF,RS_X,RS_Y+20);
  420.     g_TXT(PR_BUF,RS_X,RS_Y+30); 
  421.     g_TXT(PR_BUF,RS_X,RS_Y+40);
  422.     g_TXT(PR_BUF,RS_X,RS_Y+50);
  423. }
  424.  
  425. /********** END OF GRAPHICS FUNCTIONS **************/
  426.